package com.wy.util;

import com.wy.entity.GoodsOrder;
import com.wy.mapper.GoodsLogisticsMapper;
import com.wy.service.GoodsInfoService;
import com.wy.service.GoodsLogisticsService;
import com.wy.service.GoodsOrderService;
import com.wy.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.sql.Time;
import java.util.List;
import java.util.concurrent.*;

/**
 * @Author:王渝
 * @Date: 2022/12/31  14:08
 * @Version 1.0
 */
@Slf4j
@Configuration
public class TimeTasks {

    @Autowired
    private  GoodsInfoService goodsInfoService;

    @Autowired
    private GoodsOrderService goodsOrderService;

    @Autowired
    private EmailSender emailSender;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private GoodsLogisticsService goodsLogisticsService;


    /**
     * 定时更新结束拍商品
     */
    @Async("taskScheduler")
    @Scheduled(cron = "0/60 * * * * ?")
    public void timedUpdate(){
        //创建定时线程池
        goodsInfoService.updateStatus();
    }

    /**
     * 定时生成订单表
     */
    @Async("taskScheduler")
  @Scheduled(cron = "0/60 * * * * ?")
    public void timedGenerationOder() throws ExecutionException, InterruptedException {
        List<String> userEmails = goodsOrderService.timedInsert();
        if(userEmails != null){
        for (String userEmail : userEmails) {
            emailSender.send(userEmail, "你好!<br/>你在MyShop参与拍卖的商品交易成功<br/>请在72小时之内支付尾款");
        }
        }
    }

    /**
     * 72小时未支付为款扣去保证金  修改订单状态  增加违约次数
     */
    @Async("taskScheduler")
    @Scheduled(cron = "0/60 * * * * ?")
    public void timedChangeOrderStatus(){
        goodsOrderService.timedChangeStatus();
    }

    /**
     * 定时退还保证金
     */
    @Async("taskScheduler")
   @Scheduled(cron = "0/60 * * * * ?")
    public void timedRefundDeposit(){
        userInfoService.updateBalanceTime();
    }

    /**
     * 15天自动确认收货
     */
    @Async("taskScheduler")
     @Scheduled(cron = "0/60 * * * * ?")
    public void confirmReceipt(){
        goodsLogisticsService.autoReceive();
    }




}
